import org.springframework.context.ApplicationContext

def beanNameBase = "bean"

def bb = new grails.spring.BeanBuilder()

bb.beans {

	// Normal case
	basicBean(MyExampleBean) {
		someProperty = [8,6,7,5,3,0,"Niiiiiiiiiiiiiiiiiine"]
		name = "I'm as stupid simple as it gets!"
	}

	// Demonstrate a constructor argument
	// (Note that you can leave off {} if you don't have properties to set)
	bigInt(BigInteger, "1000")

	// Demonstrate beans created dynamically
	(1..3).each { i ->
		"${beanNameBase}${i}"(MyExampleBean) {
			someProperty = [ 1 * i,  2 * i,  3 * i ]
			name = "Dynamic Bean #${i}"
		}
	}

	// You can reference those dynamically created beans
	holder(MyExampleBeanHolder) {
		myExampleBeans = [
			ref("${beanNameBase}1"), ref("${beanNameBase}2"), ref("${beanNameBase}3")
		]
	}

	// You can use factory methods
	beanFactory(MyExampleBeanFactoryImpl)
	factoriedBean(beanFactory:"getInstance"){
		name = "factoried bean"
	}

	// You can set XML element attributes by using the closure argument
	generatorBean(MyExampleBean) { bean ->
		bean.singleton = false
		someProperty = [ "one", "night", "in", "bangkok", "will", "make", "a", "hard", "man", "crumble"]
		name = "I am a new instance each time you call me!"
	}


}

// Print out the beans
ApplicationContext appCtx = bb.createApplicationContext()
appCtx.beanDefinitionNames.each { name ->
	println "${name}: ${appCtx.getBean(name).toString()}"
}
